/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.cloud.oauthcenter.service.impl;

import com.github.pagehelper.Page;
import com.lvyh.lightframe.cloud.common.model.PageResult;
import com.lvyh.lightframe.cloud.common.model.SysOauthClient;
import com.lvyh.lightframe.cloud.common.model.SysUser;
import com.lvyh.lightframe.cloud.common.pagination.DefaultPageResult;
import com.lvyh.lightframe.cloud.common.pagination.PageParam;
import com.lvyh.lightframe.cloud.oauthcenter.bean.OauthClientDetailsBean;
import com.lvyh.lightframe.cloud.oauthcenter.service.OauthClientMainDataService;
import com.lvyh.lightframe.cloud.oauthcenter.service.SysOauthClientService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SysOauthClientServiceImpl implements SysOauthClientService {

    @Autowired
    private OauthClientMainDataService oauthClientMainDataService;

    @Override
    public SysOauthClient getByClientId(String clientId) throws Exception {
        OauthClientDetailsBean condition = new OauthClientDetailsBean();
        condition.setClientId(clientId);
        List<OauthClientDetailsBean> list = oauthClientMainDataService.selectOauthClientDetails(condition);
        if (!CollectionUtils.isEmpty(list)) {
            OauthClientDetailsBean oauthClientDetailsBean = list.get(0);
            SysOauthClient sysOauthClient = new SysOauthClient();
            BeanUtils.copyProperties(oauthClientDetailsBean, sysOauthClient);
            return sysOauthClient;
        }
        return null;
    }

    @Override
    public PageResult<SysOauthClient> queryListByPage(Map<String, Object> params) throws Exception {
        int pageNo = MapUtils.getInteger(params, "page") != null ? MapUtils.getInteger(params, "page") : 0;
        int limit = MapUtils.getInteger(params, "limit") != null ? MapUtils.getInteger(params, "limit") : 1000;
        Page<SysUser> page = new Page<>(pageNo, limit);

        OauthClientDetailsBean condition = new OauthClientDetailsBean();
        condition.setClientId(MapUtils.getString(params, "clientId"));
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page.getPageNum());
        pageParam.setPageSize(page.getPageSize());
        DefaultPageResult<OauthClientDetailsBean> pageInfo = oauthClientMainDataService.selectOauthClientDetailsByCondition(condition, pageParam);

        page.setTotal(pageInfo.getTotalCount());
        List<OauthClientDetailsBean> resultList = pageInfo.getResultList();
        List<SysOauthClient> list = convert(resultList);

        long total = page.getTotal();
        return PageResult.<SysOauthClient>builder().data(list).code(0).count(Integer.parseInt(String.valueOf(total))).build();
    }

    public List<SysOauthClient> convert(List<OauthClientDetailsBean> list) {
        List<SysOauthClient> result = new ArrayList<>();
        for (OauthClientDetailsBean bean : list) {
            SysOauthClient sysOauthClient = new SysOauthClient();
            BeanUtils.copyProperties(bean, sysOauthClient);

            sysOauthClient.setAccessTokenValiditySeconds(bean.getAccessTokenValidity());
            sysOauthClient.setRefreshTokenValiditySeconds(bean.getRefreshTokenValidity());
            sysOauthClient.setAutoapprove(bean.getAutoapprove() == 1 ? "true" : "false");
            result.add(sysOauthClient);
        }
        return result;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(SysOauthClient sysOauthClient) throws Exception {
        OauthClientDetailsBean oauthClientDetailsBean = new OauthClientDetailsBean();
        BeanUtils.copyProperties(sysOauthClient, oauthClientDetailsBean);
        int result = oauthClientMainDataService.saveOauthClientDetails(oauthClientDetailsBean);

        return result > 0 ? true : false;
    }


    @Override
    public boolean updateById(SysOauthClient sysOauthClient) throws Exception {
        OauthClientDetailsBean condition = new OauthClientDetailsBean();
        condition.setClientId(sysOauthClient.getClientId());
        List<OauthClientDetailsBean> list = oauthClientMainDataService.selectOauthClientDetails(condition);
        if (!CollectionUtils.isEmpty(list)) {
            OauthClientDetailsBean oauthClientDetailsBean = list.get(0);
            BeanUtils.copyProperties(sysOauthClient, oauthClientDetailsBean);
            return oauthClientMainDataService.updateOauthClientDetails(oauthClientDetailsBean);
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteOauthClientDetails(Integer id) throws Exception {
        return oauthClientMainDataService.deleteOauthClientDetails(id);
    }

}